1 /*
2 * Angkor Web Framework
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */
7
8 package com.tirsen.angkor.jsp;
9
10 import com.tirsen.angkor.Application;
11 import com.tirsen.angkor.RenderContext;
12 import com.tirsen.angkor.View;
13
14 import javax.servlet.ServletException;
15 import java.io.CharArrayWriter;
16 import java.io.IOException;
17 import java.io.OutputStream;
18 import java.io.OutputStreamWriter;
19 import java.io.PrintWriter;
20 import java.io.Writer;
21 import java.util.HashMap;
22 import java.util.Map;
23
24 /***
25 * <!-- $Id: PrerenderedComponentsRegistry.java,v 1.3 2002/10/09 21:37:37 tirsen Exp $ -->
26 * <!-- $Author: tirsen $ -->
27 *
28 * @author Jon Tirs´n (tirsen@users.sourceforge.net)
29 * @version $Revision: 1.3 $
30 */
31 public class PrerenderedComponentsRegistry
32 {
33 private Map prerenderedBuffers;
34 private Application application;
35 private RenderContext context;
36
37 private class PrerenderContext extends RenderContext
38 {
39 private PrintWriter writer;
40 private CharArrayWriter buffer;
41
42 public PrerenderContext()
43 {
44 super(null, null);
45 }
46
47 public String getRequestPath()
48 {
49 return context.getRequestPath();
50 }
51
52 public String encodeURL(String url)
53 {
54 return context.encodeURL(url);
55 }
56
57 public Application getApplication()
58 {
59 return application;
60 }
61
62 public PrintWriter getWriter() throws IOException
63 {
64 return writer;
65 }
66
67 public void setBuffer(CharArrayWriter buffer)
68 {
69 this.buffer = buffer;
70 this.writer = new PrintWriter(buffer);
71 }
72 }
73
74 public PrerenderedComponentsRegistry(RenderContext context)
75 {
76 this.context = context;
77 this.application = context.getApplication();
78 }
79
80 public void prerender(String name, View view) throws ServletException, IOException
81 {
82 if (prerenderedBuffers == null) prerenderedBuffers = new HashMap();
83 PrerenderContext prerenderContext = new PrerenderContext();
84 CharArrayWriter buffer = new CharArrayWriter();
85 prerenderContext.setBuffer(buffer);
86 view.render(prerenderContext);
87 prerenderedBuffers.put(name, buffer);
88 }
89
90 public void render(String name, OutputStream output) throws IOException
91 {
92 OutputStreamWriter writer = new OutputStreamWriter(output);
93 render(name, writer);
94 writer.flush();
95 }
96
97 public void render(String name, Writer writer) throws IOException
98 {
99 CharArrayWriter buffer = getBuffer(name);
100 buffer.writeTo(writer);
101 }
102
103 private CharArrayWriter getBuffer(String name)
104 {
105 if (prerenderedBuffers == null) throw new IllegalStateException("Components of this registry has not been prerendered.");
106 CharArrayWriter buffer = (CharArrayWriter) prerenderedBuffers.get(name);
107 if (buffer == null) throw new IllegalArgumentException("Component " + name + " was not prerendered for this request.");
108 return buffer;
109 }
110
111 public void reset()
112 {
113 prerenderedBuffers = null;
114 }
115 }
This page was automatically generated by Maven